package xyz.thoughtset.viewer.common.ai.model.factory;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.model.Model;
import org.springframework.ai.model.tool.ToolCallingManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import xyz.thoughtset.viewer.common.ai.model.dao.ModelParamDao;
import xyz.thoughtset.viewer.common.ai.model.entity.AiNode;
import xyz.thoughtset.viewer.common.ai.model.entity.ModelParam;
import xyz.thoughtset.viewer.common.ai.model.entity.purpose.BaseModelSetting;
import xyz.thoughtset.viewer.common.ai.model.service.AiNodeService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class ModelFactory {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private AiNodeService aiNodeService;

    @Bean
    public ToolCallingManager toolCallingManager() {
        return ToolCallingManager.builder().build();
    }


    public Model buildModel(ModelParam modelParam){
        AiNode aiNode = aiNodeService.selectDetail(modelParam.getPid());
        ModelBuilder builder = ModelsRegistry.loadBuilder(aiNode, modelParam);
        return builder.buildMode(aiNode, modelParam);
    }

    public ChatClient.Builder clientBuilder(@NonNull ModelParam modelParam){
        ChatModel chatModel = (ChatModel) buildModel(modelParam);

        return ChatClient.builder(chatModel);
    }



}
